// pages/score/batch/index.js
const {
  batchList,
  batchCategory
} = require('../../../api/api')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentRegion: undefined,
    keywords: '',
    list: [],
    cityList: [],
    subjectData:[],
    yearData:[],
    isVisible: false,
    province: {
      value: '福建',
      code:''
    },
    year: {
      value: '',
    },
    subject:{
      id:'',
      value:'',
    },
    showProvince:false,
    showYear:false,
    showSubject:false,
    yearList:[]
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      province:{value:wx.getStorageSync('userCitys').name,id:wx.getStorageSync('userCitys').code}
    })
    this.getCityList()
    // this.getList()
    this.getPlanCategory()
  },
    // 获取 年份-类型
    async getPlanCategory() {
      let {
        province,
        subjectData,
         yearData,
         year,
         subject,
      } = this.data
      const {
        result
      } = await batchCategory(province.id)
      result.sort((a, b) => {
        return b.year - a.year
      }) //倒序
      if (result.length > 0) {
        result.map((item) => {
          item.list.map((e, i) => {
            let obj = {
              id: e,
              value: e
            }
            item.list[i] = obj
          })
          item.list.push({
            id:'',
            value: '不限',
          })
        })
        var yearArr = result.map((item) => {
          return {
            value: item.year,
          }
        })
  
        const {
          list
        } = result[0]
        year.value = yearArr[0].value
        subject.id = list[0].id
        subject.value = list[0].value
        subjectData= list
        yearData=yearArr
      }
      this.setData({
        year,
        subject,
        subjectData,
        yearData,
        data:result
      })
       this.getList()
    },
    
  async getCityList() {
    const result=wx.getStorageSync('cityList')||await getApp().getCityList()
    const cityList=result.map((item)=>{
      return {
        id:item.id,
        value:item.title
      }
    })
    this.setData({
      cityList
    })

  },
  toggle(e){
    const {
      value,
      key  
    } = e.currentTarget.dataset
    this.setData({
      showProvince:false,
      showSubject:false,
      showYear:false
    })
    this.setData({
      [key]: !value
    })
  },
  confirm(e) {
    const {
      value,
      key,
    } = e.detail
    let  {
      data,
      subjectData,
      subject
     }=this.data
     if((this.data[key].value==value['value'])) return
        this.setData({
          [key]: value
        })
        if(key=='province'){
          this.getPlanCategory()
          return 
        } 
      if(key=='year'){
          var list = []
          data.map((item) => {
            if (item.year == value['value']) {
                list = item.list
                subjectData=list
                subject.id=list[0].id
                subject.value=list[0].value
                this.setData({
                  subject,
                  subjectData
                })
            }
          })
        }
      this.getList()
  },
  async getList() {
    const {province,year,subject}=this.data
    const params = {
      cityCode: province.id,
      year: year.value,
      category:subject.id,
    }
    const {
      result
    } = await batchList(params)
    // console.log(params)
    this.setData({
      list: result,
      yearList:result.length>0?result[0].yearList:[]
    })
  },
  chooseCity() {
    wx.navigateTo({
      url: '/pages/list/index?type=2&category=city',
    })
  },
  searchInput(e) {
    this.setData({
      keywords: e.detail.value
    })
  },
  search() {
    if (this.data.keywords.length == 0) return
    this.getList()
  },
  onShareAppMessage(){
    return {
      title: '批次查询',
      imageUrl: 'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/shareImg.jpg'
    }
  }
})